import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { CravingRecords, CravingRecord } from '@/types/global'

export const useSmokingHistoryStore = defineStore('smokingHistory', () => {
  const quitDate = ref('')
  const dailyCigarettes = ref(20)
  const pricePerPack = ref(15)
  const cigarettesPerPack = ref(20)
  const cravingRecords = ref<CravingRecords>({})
  function setQuitDate(date: string) {
    quitDate.value = date
  }
  function addCravingRecord(record: CravingRecord) {
    const today = new Date().toISOString().split('T')[0]
    if (!cravingRecords.value[today]) {
      cravingRecords.value[today] = []
    }
    cravingRecords.value[today].push(record)
  }
  
  function clear() {
    quitDate.value = ''
    dailyCigarettes.value = 20
    pricePerPack.value = 15
    cigarettesPerPack.value = 20
    cravingRecords.value = {}
  }
  return {
    quitDate,
    dailyCigarettes,
    pricePerPack,
    cigarettesPerPack,
    cravingRecords,
    setQuitDate,
    addCravingRecord,
    clear
  }
}, { persist: true })